package com.qf.controller;

import com.qf.annotations.RequestMapping;
import com.qf.pojo.CartItem;
import com.qf.pojo.TbUser;
import com.qf.service.CartService;
import com.qf.service.Impl.CartServiceImpl;
import com.qf.service.Impl.gouwucheserviceImpl;
import com.qf.service.gouwucheservice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RequestMapping("/cart")
public class CartController {
    CartService cartService = new CartServiceImpl();

    @RequestMapping("/create")
    public String addCart(HttpServletRequest request, HttpServletResponse response) {
        String goodId = request.getParameter("id");
        TbUser loginUser = (TbUser) request.getSession().getAttribute("loginUser");

        long uid = loginUser.getId();
        int num = 1;
        try {
            cartService.addCart(goodId, uid, num);
            response.setContentType("text/html;charset=utf-8");
            return "redirect:/cartSuccess.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            return "redirect:/error/error.jsp";
        }
    }

    @RequestMapping("/show")
    public String show(HttpServletRequest request, HttpServletResponse response) {
        if (request.getSession().getAttribute("cart") == null) {
            TbUser loginUser = (TbUser) request.getSession().getAttribute("loginUser");
            long uid = loginUser.getId();
            List<CartItem> cartByUid = cartService.findCartByUid(uid);
            request.setAttribute("cart", cartByUid);
        }
        return "forward:/cart.jsp";
    }

}
